package be.ipl.albums.daoimpl;

import java.util.Calendar;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.TemporalType;

import be.ipl.albums.dao.AlbumDao;
import be.ipl.albums.domaine.Album;
import be.ipl.albums.domaine.Album.Categorie;
import be.ipl.albums.domaine.AlbumPK;

@SuppressWarnings("serial")
@Stateless
public class AlbumDaoImpl extends DaoImpl<AlbumPK, Album> implements AlbumDao {

	@Override
	public List<Album> listerDepuis(Calendar date) {
		String query = "select a from Album a WHERE a.dateAjout <= ?1";
		return super.liste(query, date, TemporalType.DATE);
	}

	@Override
	public List<Album> lister(String mot) {

		String[] mots = mot.split(" ");
		
		if (mots.length != 1) {
			String query = "select a from Album a WHERE lower(a.pk.nom) LIKE ?1 OR lower(a.pk.nom) LIKE ?2 OR lower(a.pk.artiste) LIKE ?1 OR lower(a.pk.artiste) LIKE ?2";
			for (String s : mots) {
				s = "%" + s + "%";
			}
			return super.liste(query, mots[0], mots[1]);
		}
		String query = "select a from Album a WHERE lower(a.pk.nom) LIKE ?1 OR lower(a.pk.artiste) LIKE ?1";
		mot = "%" + mot + "%";
		return super.liste(query, mot);
	}

	@Override
	public List<Album> lister(Categorie categorie) {
		String query = "select a from Album a WHERE a.categorie = ?1 ORDER BY a.dateAjout DESC, a.pk.artiste ASC";
		return super.liste(query, categorie);
	}

	@Override
	public List<Album> listeOrdonnee() {
		String query = "select a from Album a ORDER BY a.dateAjout DESC, a.pk.artiste";
		return super.liste(query);
	}

	@Override
	public List<Album> listeDuJour(Calendar date) {
		String dateAjd = date.get(Calendar.YEAR) + "-"
				+ date.get(Calendar.MONTH) + "-"
				+ date.get(Calendar.DAY_OF_MONTH);
		String query = "select a from Album a WHERE a.dateAjout = to_date(?1,'YYYY-MM-DD')";
		return super.liste(query, dateAjd);
	}

}
